<template>
  <div class="ref-goods" v-for="item in recommendData?.children" :key="item.id">
    <div class="head">
      <h3>- {{ item.name }} -</h3>
      <p class="tag">温暖柔软，品质之选</p>
      <XtxMore />
    </div>
    <div class="body">
      <GoodsItem
        v-for="value in item.goods"
        :key="value.id"
        :valueItem="value"
      />
    </div>
  </div>
</template>

<script>
import GoodsItem from "./GoodsItem";
import { getOneRecommend } from "../../api/category";
import { useRoute, onBeforeRouteUpdate } from "vue-router";
import { ref } from "vue";

export default {
  name: "CommandGoods",

  setup() {
    let { recommendData, getData } = fn();
    const route = useRoute();
    getData(route.params.id);

    // 当路由更新的时候执行
    onBeforeRouteUpdate((to) => {
      // console.log(to);
      getData(to.params.id);
    });

    return {
      recommendData,
    };
  },
  components: { GoodsItem },
};

// 获取推荐数据
function fn() {
  // 存储推荐数据
  let recommendData = ref([]);

  function getData(id) {
    getOneRecommend(id).then((res) => {
      recommendData.value = res.result;
    });
  }

  return { recommendData, getData };
}
</script>

<style lang="less" scoped>
.ref-goods {
  background-color: #fff;
  margin-top: 20px;
  position: relative;

  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }

  .head {
    .xtx-more {
      position: absolute;
      top: 20px;
      right: 20px;
    }

    .tag {
      text-align: center;
      color: #999;
      font-size: 20px;
      position: relative;
      top: -20px;
    }
  }

  .body {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0 65px 30px;
  }
}
</style>
